﻿
//表单附件选择插件
(function ($, ibpm) {
    "use strict";
    $.BPM_Uploader = {
        init: function ($self) {
            var dfop = $self[0]._MUploader.dfop;
            $.BPM_Uploader.initRender($self, dfop);
        },
        initRender: function ($self, dfop) {
            $self.attr('type', 'upload').addClass('M-Uploader-wrap');
            var $wrap = $('<div class="M-Uploader-input" ></div>');

            var $btnGroup = $('<div class="M-Uploader-btn-group"></div>');
            var $uploadBtn = $('<a id="M-Uploader_uploadBtn_' + dfop.id + '" class="btn btn-success M-Uploader-input-btn">上传</a>');
            var $downBtn = $('<a id="M-Uploader_downBtn_' + dfop.id + '" class="btn btn-danger M-Uploader-input-btn">下载</a>');

            $self.append($wrap);
            if (dfop.isUpload)
                $btnGroup.append($uploadBtn);
            if (dfop.isDown)
                $btnGroup.append($downBtn);

            $self.append($btnGroup);

            if (dfop.isUpload)
                $uploadBtn.on('click', $.BPM_Uploader.openUploadForm);
            if (dfop.isDown)
                $downBtn.on('click', $.BPM_Uploader.openDownForm);

            if (!!dfop.value) {
                setTimeout(function () {
                    ibpm.httpAsyncGet(top.$.rootUrl + '/Annexes/GetFileNames?id=' + dfop.value, function (res) {
                        if (res.code == ibpm.httpCode.success) {
                            $('#' + dfop.id).find('.M-Uploader-input').text(res.info);
                        }
                    });
                }, 300);
            }
        },
        openUploadForm: function () {
            var $btn = $(this);
            var $self = $btn.parents('.M-Uploader-wrap');
            var dfop = $self[0]._MUploader.dfop;
            ibpm.layerForm({
                id: 'layer_' + dfop.id,  //id不能重复
                title: dfop.placeholder,
                url: top.$.rootUrl + '/Annexes/UploadForm?keyVaule=' + dfop.value + "&extensions=" + dfop.extensions,
                width: 800,
                height: 600,
                maxmin: true,
                btn: null,
                end: function () {
                    ibpm.httpAsyncGet(top.$.rootUrl + '/Annexes/GetFileNames?id=' + dfop.value, function (res) {
                        if (res.code == ibpm.httpCode.success) {
                            $('#' + dfop.id).find('.M-Uploader-input').text(res.info);
                        }
                    });
                }
            });
        },
        openDownForm: function () {
            var $btn = $(this);
            var $self = $btn.parents('.M-Uploader-wrap');
            var dfop = $self[0]._MUploader.dfop;
            ibpm.layerForm({
                id: 'layer_' + dfop.id,
                title: dfop.placeholder,
                url: top.$.rootUrl + '/Annexes/DownForm?keyVaule=' + dfop.value,
                width: 800,
                height: 600,
                maxmin: true,
                btn: null
            });
        }
    };

    $.fn.ibpm_Uploader = function (op) {
        var $this = $(this);
        if (!!$this[0]._MUploader) {
            return $this;
        }
        var dfop = {
            placeholder: '上传附件',
            isUpload: true,
            isDown: true,
            value: false,
            extensions: ''
        }

        $.extend(dfop, op || {});
        dfop.id = $this.attr('id');
        if (!dfop.value) {
            dfop.value = ibpm.newGuid();
        }

        $this[0]._MUploader = { dfop: dfop };
        $.BPM_Uploader.init($this);
    };

    $.fn.ibpm_UploaderSet = function (value) {
        var $self = $(this);
        var dfop = $self[0]._MUploader.dfop;
        dfop.value = value;
        ibpm.httpAsyncGet(top.$.rootUrl + '/Annexes/GetFileNames?id=' + dfop.value, function (res) {
            if (res.code == ibpm.httpCode.success) {
                $('#' + dfop.id).find('.M-Uploader-input').text(res.info);
            }
        });
    }

    $.fn.ibpm_UploaderGet = function () {
        var $this = $(this);
        var dfop = $this[0]._MUploader.dfop;
        return dfop.value;
    }
})(jQuery, top.ibpm);